package cn.monkey.family.server.domain.uc.role;

import cn.monkey.family.data.uc.role.RoleMapper;
import cn.monkey.family.data.uc.role.datascope.RoleDataScopeDefinitionRepository;
import cn.monkey.family.data.uc.role.resource.RoleResources;
import cn.monkey.family.data.uc.role.resource.RoleResourcesRepository;
import cn.monkey.family.server.domain.uc.resource.ResourceRepository;
import cn.monkey.family.server.domain.uc.role.resource.RoleResourceRelRepository;
import org.mapstruct.factory.Mappers;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;

@Configuration
public class RoleConfig {

    @Bean
    RoleResourcesRepository roleResourcesRepository(RoleResourceRelRepository roleResourceRelRepository,
                                                    ResourceRepository resourceRepository) {
        return roleId -> roleResourceRelRepository.selectByRoleId(roleId).map(RoleResources::getResourceIds)
                .map(resourceRepository::findAllById)
                .orElse(Collections.emptyList());
    }

    @Bean
    RoleDataScopeDefinitionRepository roleDataScopeDefinitionRepository() {
        return roleId -> Collections.emptyList();
    }

    @Bean
    RoleMapper roleMapper() {
        return Mappers.getMapper(RoleMapper.class);
    }

}
